package com.example.reagent_management.ui.operatedialog.dialogs

import android.widget.Button
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.fragment.app.viewModels
import com.example.lib_base.base.BaseDBDialogFragment
import com.example.lib_base.base.DataBindingConfig
import com.example.lib_base.utils.DateConvertUtils
import com.example.reagent_management.BR
import com.example.reagent_management.R
import com.example.reagent_management.databinding.FragmentBottomHistoryBinding
import com.example.reagent_management.room.entity.HistoryInfo
import com.example.reagent_management.ui.operatedialog.viewmodle.BottomHistoryViewModel
import com.hjq.toast.ToastUtils
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class BottomHistoryFragment : BaseDBDialogFragment() {

    private val mState: BottomHistoryViewModel by viewModels()

    override fun getDataBindingConfig(): DataBindingConfig? {
        return DataBindingConfig(R.layout.fragment_bottom_history,BR.VM,mState)
    }

    override fun initViewOrObserve() {
        setTransAndHeight(true, 1f)
        setOnDataListener {
            mState.history.value = (it as HistoryInfo)
        }
        mState.history.observe(viewLifecycleOwner) {
            mState.setAllData()
        }
        (getBinding() as FragmentBottomHistoryBinding).apply {
            close.setOnClickListener {
                mainMotion.clearAnimation()
                //直接设置为原来的状态
                mainMotion.transitionToState(R.id.start)
                mainMotion.addTransitionListener(setTransitionListener())
            }
            val deleteSelect =  deleteChoose.findViewById<Button>(R.id.delete)
            val backSelect = deleteChoose.findViewById<Button>(R.id.cancel)
            deleteSelect.setOnClickListener {
                deleteSelect.isClickable = false
                backSelect.isClickable = false
                val time = DateConvertUtils.convertServiceTypeToMill(mState.history.value?.times )
                if (System.currentTimeMillis() - time <= 86400000){
                    mState.rollBack()
                    mState.success.observe(viewLifecycleOwner,{
                        if (it){
                            ToastUtils.show("删除成功")
                            dismissAllowingStateLoss()
                        }else {
                            ToastUtils.show("删除失败\n，试剂已删除/其他原因")
                            deleteSelect.isClickable = true
                            backSelect.isClickable = true
                        }
                    })
                }else {
                    ToastUtils.show("超过24h不允许回退")
                    deleteSelect.isClickable = true
                    backSelect.isClickable = true
                }
            }
        }
    }

    private fun setTransitionListener(): MotionLayout.TransitionListener {
        return object : MotionLayout.TransitionListener {
            override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) {

            }

            override fun onTransitionChange(p0: MotionLayout?, p1: Int, p2: Int, p3: Float) {

            }

            override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) {
                dismissAllowingStateLoss()
            }

            override fun onTransitionTrigger(
                p0: MotionLayout?,
                p1: Int,
                p2: Boolean,
                p3: Float
            ) {

            }
        }
    }


}